// 侧边栏激活状态管理
document.addEventListener('DOMContentLoaded', function() {
    // 获取当前页面路径
    const currentPath = window.location.pathname;
    const filename = currentPath.split('/').pop();
    
    // 查找所有导航链接
    const navLinks = document.querySelectorAll('.sidebar-menu a');
    
    // 设置活动链接
    navLinks.forEach(link => {
        const linkHref = link.getAttribute('href');
        if (linkHref === filename || 
            (filename === '' && linkHref === 'qd-display.html') || 
            (filename === 'index.html' && linkHref === 'qd-display.html') ||
            currentPath.includes(linkHref)) {
            link.classList.add('active');
        } else {
            link.classList.remove('active');
        }
    });
    
    // 初始化工具提示
    initTooltips();
    
    // 初始化按钮点击效果
    initButtonEffects();
});

// 初始化工具提示
function initTooltips() {
    const tooltips = document.querySelectorAll('.tooltip');
    
    tooltips.forEach(tooltip => {
        const tooltipText = tooltip.querySelector('.tooltiptext');
        if (!tooltipText) return;
        
        tooltip.addEventListener('mouseenter', () => {
            tooltipText.style.visibility = 'visible';
            tooltipText.style.opacity = '1';
        });
        
        tooltip.addEventListener('mouseleave', () => {
            tooltipText.style.visibility = 'hidden';
            tooltipText.style.opacity = '0';
        });
    });
}

// 初始化按钮点击效果
function initButtonEffects() {
    const buttons = document.querySelectorAll('.btn, .chart-btn');
    
    buttons.forEach(btn => {
        btn.addEventListener('click', () => {
            // 添加点击效果
            btn.style.transform = 'scale(0.95)';
            setTimeout(() => {
                btn.style.transform = '';
            }, 100);
        });
    });
}

// 显示通知信息
function showNotification(message, type = 'info') {
    // 创建通知元素
    const notification = document.createElement('div');
    notification.className = `notification ${type}`;
    
    // 添加图标
    let icon = '';
    switch(type) {
        case 'success':
            icon = '<i class="fas fa-check-circle"></i>';
            break;
        case 'warning':
            icon = '<i class="fas fa-exclamation-triangle"></i>';
            break;
        case 'error':
            icon = '<i class="fas fa-times-circle"></i>';
            break;
        default:
            icon = '<i class="fas fa-info-circle"></i>';
    }
    
    // 设置内容
    notification.innerHTML = `
        <div class="notification-icon">${icon}</div>
        <div class="notification-message">${message}</div>
        <div class="notification-close"><i class="fas fa-times"></i></div>
    `;
    
    // 添加到页面
    if (!document.querySelector('.notification-container')) {
        const container = document.createElement('div');
        container.className = 'notification-container';
        document.body.appendChild(container);
    }
    
    document.querySelector('.notification-container').appendChild(notification);
    
    // 自动关闭
    setTimeout(() => {
        notification.classList.add('hide');
        setTimeout(() => {
            notification.remove();
        }, 300);
    }, 5000);
    
    // 点击关闭
    notification.querySelector('.notification-close').addEventListener('click', () => {
        notification.classList.add('hide');
        setTimeout(() => {
            notification.remove();
        }, 300);
    });
}

// 格式化日期
function formatDate(date) {
    if (!date) return '';
    
    if (typeof date === 'string') {
        date = new Date(date);
    }
    
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 创建确认对话框
function createConfirmDialog(title, message, onConfirm, onCancel) {
    // 创建对话框元素
    const dialog = document.createElement('div');
    dialog.className = 'dialog-overlay';
    
    dialog.innerHTML = `
        <div class="dialog-content">
            <div class="dialog-header">
                <h3>${title}</h3>
                <button class="dialog-close"><i class="fas fa-times"></i></button>
            </div>
            <div class="dialog-body">
                <p>${message}</p>
            </div>
            <div class="dialog-footer">
                <button class="btn btn-secondary dialog-cancel">取消</button>
                <button class="btn btn-primary dialog-confirm">确定</button>
            </div>
        </div>
    `;
    
    // 添加到页面
    document.body.appendChild(dialog);
    
    // 阻止冒泡
    dialog.querySelector('.dialog-content').addEventListener('click', (e) => {
        e.stopPropagation();
    });
    
    // 点击关闭
    const closeBtn = dialog.querySelector('.dialog-close');
    closeBtn.addEventListener('click', () => {
        dialog.remove();
        if (onCancel) onCancel();
    });
    
    // 点击取消
    const cancelBtn = dialog.querySelector('.dialog-cancel');
    cancelBtn.addEventListener('click', () => {
        dialog.remove();
        if (onCancel) onCancel();
    });
    
    // 点击确定
    const confirmBtn = dialog.querySelector('.dialog-confirm');
    confirmBtn.addEventListener('click', () => {
        dialog.remove();
        if (onConfirm) onConfirm();
    });
    
    // 点击背景关闭
    dialog.addEventListener('click', () => {
        dialog.remove();
        if (onCancel) onCancel();
    });
}

// 加载动画
function showLoading(container) {
    const loading = document.createElement('div');
    loading.className = 'loading-spinner';
    loading.innerHTML = '<div class="spinner"></div>';
    
    if (container) {
        container.appendChild(loading);
    } else {
        document.body.appendChild(loading);
    }
    
    return loading;
}

function hideLoading(loading) {
    if (loading && loading.parentNode) {
        loading.parentNode.removeChild(loading);
    }
} 